package futian 
{
	import flash.text.TextField;
	import flash.filesystem.File;
	public class Datas
	{
		public static var index:int = 0;
		public static var list:Array;

		private static var curYear:int = 0;
		private static var curMon:int = 0;
		private static var curDay:int = 0;

		private static const filename:String = "futian/签到统计表.txt";
		private static const jsonFile:String = "futian/config.json";

		private static var _jsonObject:Object;
		public static function get jsonObject():Object
		{
			if(_jsonObject==null)
			{
				logs.adds(jsonFile);
				_jsonObject = SwfLoader.readJsonFile(jsonFile);
			}
			return _jsonObject;
		}

		public static function get xiangkuangArr():Array
		{
			if (jsonObject.xiangkuang == null)
			{
				jsonObject.xiangkuang= new Array;
				//{"name":"番薯","icon":"futian/New/1.png","video":"test.mp4","url":"https://www.baidu.com/s?wd=番薯&ie=UTF-8"},
				//获取文件夹下图片，视频图信息
				var picturesArr:Array = SwfLoader.filesInDir("futian/xiangkuang", ".*\.png$", false);
				for each (var f:File in picturesArr)
				{
					var obj:Object = new Object;
					obj.icon = f.url;
					obj.name = f.name.replace(/^([^\.]+)\..*/, "$1");
					logs.adds("->",f.name);

					//obj.video = getVideoURL(obj);//vid_url + file.name + "/" + f.name.replace(/png$/, "flv");
					//obj.url = getCodeURL(obj);//_url + file.name + "/" + f.name.replace(/png$/, "jpg");
					jsonObject.xiangkuang.push(obj);
				}

			}
			return jsonObject.xiangkuang;
		}

		//"countDown":1,//拍照倒计时读秒开始读数,默认值为5,(单位：秒)
		public static function get countDown():int
		{
			if(jsonObject.countDown>0)
				return jsonObject.countDown;
			return 5;
		}
		//"quitTime":10,//无人操作回到首页的时间,默认值为60,(单位：秒)
		public static function get quitTime():int
		{
			if(jsonObject.quitTime != undefined)
				return jsonObject.quitTime;
			return 60;
		}
		//"showPrintDialog":true,//打印时是否显示设置打印弹窗，true：显示，false：不显示
		public static function get showPrintDialog():Boolean
		{
			if(jsonObject.showPrintDialog != undefined)
				return jsonObject.showPrintDialog;
			return false;
		}
		public static function get signTitle():String
		{
			if(jsonObject.signTitle!= undefined)
				return jsonObject.signTitle;
			return "";
		}
		public static function get willPlayPictures():Boolean
		{
			if(jsonObject.quitTime >0)
				return false;
			return true;
		}

		//"rate":1.48,//打印纸张的高宽比，默认值1.48,(高度除以宽度的值)
		public static function get rate():Number
		{
			if(jsonObject.rate>0)
				return jsonObject.rate;
			return 1.48;
		}
		//"paperWidth":700,//打印纸张像素宽度,应大于打印模板图片的像素宽度,默认值700
		public static function get paperWidth():int
		{
			if(jsonObject.paperWidth>0)
				return jsonObject.paperWidth;
			return 700;
		}
		//"margin":34,//打印纸张的边缘空白像素值,值越大边缘空白越大，海报打印区越小,默认值34
		public static function get margin():Number
		{
			if(jsonObject.marginRate>0)
				return jsonObject.marginRate;
			return 0.05;
		}

		public static function setIndexTxt(txt:TextField):TextField
		{
			if(txt==null)
				txt = ViewSet.makeTxtLine("您是第"+index+"位签到来宾",40,0xa41c20);
			txt.text = "您是第"+index+"位签到来宾";
			txt.x = (1920-txt.width)/2;
			txt.y = 20;
			txt.visible = Boolean(index>0);
			return txt;
		}

		public function Datas()
		{
		}
		public static function init():void
		{
			var date:Date = new Date();
			curYear = int(date.getFullYear());
			curMon = int(date.getMonth()+1);
			curDay = int(date.getDate());
			index = 0;
			str2list(WriteData.read_txt(filename));
		}
		private static function isTodayChanged():Boolean
		{
			var date:Date = new Date();
			if(int(date.getFullYear())==curYear && int(date.getMonth()+1)==curMon && int(date.getDate())==curDay)
				return false;
			return true;
		}
		public static function countNext():void
		{
			if(isTodayChanged())
			{
				init();
			}
			++index;
		}

		public static function saveToFile():void
		{
			var s:String = list2str();
			WriteData.write_txt(filename,s);
		}

		public static function str2list(s:String):Array
		{
			list = null;
			if(s==null)
				return list;
			s = s.replace(/[\r\n]/g,"\n");
			var arr:Array = s.split(/[\r\n]/);
			for each(var line:String in arr)
			{
				var items:Array = line.match(/[\d]+/g);
				logs.adds("line: "+line," items: ",items);
				if(items && items.length==4)
				{
					if(list==null)
						list = new Array();
					if(isLineToday(items))
					{
						logs.adds("today=====",line);
						index = items[3];
					}
					list.push(items);
				}
			}
			return list;

		}
		public static function list2str():String
		{
			var s:String = "时间\t\t签到人数";
			s +="\r\n" + date2str()+"\t"+index;
			if(list==null)
				return s;
			for each(var items:Array in list)
			{
				if(!isLineToday(items))
				{
					s +="\r\n" + date2str(items[0],items[1],items[2])+"\t"+items[3];
				}
			}
			return s;
		}
		public static function isLineToday(items:Array):Boolean
		{
			if(items && items.length==4)
			{
				if(curYear==int(items[0]) && curMon==int(items[1]) && curDay==int(items[2]))
					return true;
			}
			return false;
		}
		public static function date2str(y:int=0,m:int=0,d:int=0):String
		{
			if(y==0 && m==0 && d==0){
				return curYear + "年" + curMon +"月" + curDay +"日";
			}
			return y + "年" + m +"月" + d +"日";
		}
		public static function sumOfDay(date:Array):int
		{
			if(date && date.length>=3)
			{
				for each(var items:Array in list)
				{
					if(
							int(items[0])==date[0]
							&& int(items[1])==date[1]
							&& int(items[2])==date[2]
					  )
					{
						return int(items[3]);
					}
				}
			}
			return 0;
		}

		public static function isEqualAfter(date0:Array,date1:Array):Boolean
		{
			if(
					date0 && date0.length>=3 &&
					date1 && date1.length>=3 
			  )
			{
				var d1:int = int(date0[0])*366 + int(date0[1]) * 31 + int(date0[2]);
				var d2:int = int(date1[0])*366 + int(date1[1]) * 31 + int(date1[2]);
				logs.adds(d1,d2,d2>=d1);
				return Boolean(d2>=d1);
			}
			return false;
		}

		public static function sumFromTo(date0:Array,date1:Array):int
		{
			if(
					date0 && date0.length>=3 &&
					date1 && date1.length>=3 
			  )
			{
				var i:int = 0;
				for each(var items:Array in list)
				{
					if(isEqualAfter(date0,items) && isEqualAfter(items,date1))
						i += int(items[3]);
				}
				return i;
			}
			return 0;
		}
	}
}
